package com.dark.util;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.locks.ReentrantLock;

/**
 * ClassName: ScheduleUtil
 * Description:进度工具，执行进度相关
 *
 * @author chengluchao
 * @date: 2022/9/19 9:11 AM
 * @since 1.0.9
 */
public abstract class ScheduleUtil {
    static final transient ReentrantLock lock = new ReentrantLock();

    public static String printProgressBar(int size, int total) {
        return printProgressBar(calculatePercentage(size, total));
    }

    public static String printProgressBar(long size, long total) {
        return printProgressBar(calculatePercentage((int) size, (int) total));
    }

    /**
     * 打印进度条*
     *
     * @param schedule 百分比
     * @return s
     */
    public static String printProgressBar(int schedule) {
        lock.lock();
        try {
            // 打印进度
            StringBuilder knead = new StringBuilder();
            knead.append("[");
            for (int i = 0; i < 100; i++) {
                if (schedule > i) {
                    knead.append(">");
                } else {
                    knead.append("_");
                }
            }
            knead.append(schedule + "%");
            knead.append("]");
            return knead.toString();
        } finally {
            lock.unlock();
        }

    }

    /**
     * 计算百分比*
     *
     * @param total 总数
     * @param size  进度
     * @return b
     */
    public static int calculatePercentage(int size, int total) {
        return (int) (size / (double) total * 100);
    }


}
